class Solution {
    public int findKthLargest(int[] nums, int k) {
        /*int n = nums.length;
        Arrays.sort(nums);
        return nums[n - k];*/
        
        PriorityQueue<Integer> pq = new PriorityQueue<>();

        // Time complexity: O(n log2(k))
        // Space complexity: O(k)
        for(int num : nums) { // n iterations
            pq.add(num); // O(log2(k))
            if(pq.size() > k) {
                pq.remove(); // O(log2(k))
            }
        }

        return pq.peek();
        
    }
}